package com.hitqz.robot.api.business.component;

import cn.hutool.core.util.StrUtil;
import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import net.coobird.thumbnailator.Thumbnails;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MyStringImageConverter implements Converter<String> {
    @Override
    public Class supportJavaTypeKey() {
        return String.class;
    }

    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return CellDataTypeEnum.STRING;
    }


    // 图片失效处理
    @Override
    public WriteCellData<?> convertToExcelData(String value, ExcelContentProperty contentProperty,
                                            GlobalConfiguration globalConfiguration)
            throws IOException {
        ByteArrayOutputStream handlerOutputStream= null;
        WriteCellData var6;
        try {
            if (StrUtil.isBlank(value)) {
                return new WriteCellData("");
            }
            if (value.contains(".mp4") || value.contains(".m4a") || value.contains(".wav")){
                return new WriteCellData("");
            }
            handlerOutputStream = new ByteArrayOutputStream();
            if (StrUtil.isNotBlank(value) && value.contains("http")){
                URL urlValue = new URL(encode(value, "utf8"));
                Thumbnails.of(urlValue).size(640,480).
                        outputQuality(1f).
                        outputFormat("jpg").toOutputStream(handlerOutputStream);
            }else {
                File file = new File(value);
                Thumbnails.of(file).size(640,480).
                        outputQuality(1f).
                        outputFormat("jpg").toOutputStream(handlerOutputStream);
            }
            byte[] bytes = handlerOutputStream.toByteArray();
            var6 = new WriteCellData(bytes);

            return var6;
        } catch (IOException exception) {
            return new WriteCellData("");
        } finally {
            if (handlerOutputStream != null) {
                handlerOutputStream.close();
            }
        }
    }

    private static String zhPattern = "[\\u4e00-\\u9fa5]";

    /**
     * 替换字符串卷
     *
     * @param str     被替换的字符串
     * @param charset 字符集
     * @return 替换好的
     * @throws UnsupportedEncodingException 不支持的字符集
     */
    public static String encode(String str, String charset) throws UnsupportedEncodingException {
        Pattern p = Pattern.compile(zhPattern);
        Matcher m = p.matcher(str);
        StringBuffer b = new StringBuffer();
        while (m.find()) {
            m.appendReplacement(b, URLEncoder.encode(m.group(0), charset));
        }
        m.appendTail(b);
        return b.toString();
    }

}
